package com.oodso.say.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.oodso.say.utils.HttpSubscriber;
import com.oodso.say.utils.SignoutUtils;
import com.zhy.autolayout.AutoLayoutActivity;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observable;

//import com.umeng.message.PushAgent;

/**
 * 基类  需继承
 */
public abstract class BaseActivity extends AutoLayoutActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private Unbinder unbinder;

    protected abstract void initUI();
    protected abstract void initData();

    private List<HttpSubscriber> subscriptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SignoutUtils.getInstance().addActivity(this);

//        PushAgent.getInstance(this).onAppStart();
        initUI();
        initData();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        this.injectViews();
    }


    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        injectViews();
    }


    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        injectViews();
    }

    private void injectViews() {
        unbinder = ButterKnife.bind(this);
        Fresco.initialize(this);
        EventBus.getDefault().register(this);
    }

    public void addSubscriptions(HttpSubscriber subscriber) {
        if (null == subscriptions)
            subscriptions = new ArrayList<>();
        subscriptions.add(subscriber);
    }

    protected void subscribe(Observable o, HttpSubscriber h) {
        o.subscribe(h);
        addSubscriptions(h);
    }

    public void unSubscribes() {
        if (null != subscriptions && subscriptions.size() > 0) {
            for (int i = 0; i < subscriptions.size(); i++) {
                if (!subscriptions.get(i).isUnsubscribed()) {
                    subscriptions.get(i).unsubscribe();
                    subscriptions.get(i).onError(new Throwable("请求取消"));
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        unSubscribes();
        SignoutUtils.getInstance().destroy(this);
        super.onDestroy();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 删除Fragment,但是并不出栈
     */
    public void removeFragment(Fragment fragment) {
        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.remove(fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 删除Fragment,而且是弹出栈，彈出頂部棧Fragment
     */
    public void removeFragment() {
        getSupportFragmentManager().popBackStackImmediate();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void addFragmentAddStack(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(fragmentRoot(), fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void addFragmentAddStack(Fragment fragment, int addLayID) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(addLayID, fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);
     */
    public void addFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(fragmentRoot(), fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);
     */
    public void addFragment(Fragment fragment, int addLayID) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(addLayID, fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void replaceFragmentAddStack(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(fragmentRoot(), fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void replaceFragmentAddStack(Fragment fragment, String tag) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(fragmentRoot(), fragment, tag);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void replaceFragmentAddStack(Fragment fragment, int replaceLayID) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(replaceLayID, fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);
     */
    public void replaceFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(fragmentRoot(), fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);
     */
    public void replaceFragment(Fragment fragment, int replaceLayID) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(replaceLayID, fragment);
        ft.commitAllowingStateLoss();
    }

    /*********************************************************** 动画区域 Start **************************/
    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void addFragmentAddStack(Fragment fragment, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.add(fragmentRoot(), fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈
     */
    public void addFragmentAddStack(Fragment fragment, int addLayID, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.add(addLayID, fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);
     */
    public void addFragment(Fragment fragment, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.add(fragmentRoot(), fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);
     */
    public void addFragment(Fragment fragment, int addLayID, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);

        ft.add(addLayID, fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈，可以设置动画
     */
    public void replaceFragmentAddStack(Fragment fragment, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.replace(fragmentRoot(), fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈，可以设置动画
     */
    public void replaceFragmentAddStack(Fragment fragment, int enter, int exit, int popEnter, int popExit, String tag) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.replace(fragmentRoot(), fragment, tag);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 执行 ft.addToBackStack(null); 圧栈，返回的时候弹出栈，可以设置动画
     */
    public void replaceFragmentAddStack(Fragment fragment, int replaceLayID, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.replace(replaceLayID, fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);,可以设置动画
     */
    public void replaceFragment(Fragment fragment, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.replace(fragmentRoot(), fragment);
        ft.commitAllowingStateLoss();
    }


    /**
     * 不执行 ft.addToBackStack(null);可以设置动画
     */
    public void replaceFragment(Fragment fragment, int replaceLayID, int enter, int exit, int popEnter, int popExit) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(enter, exit, popEnter, popExit);
        ft.replace(replaceLayID, fragment);
        ft.commitAllowingStateLoss();
    }


    public abstract int fragmentRoot();

    public abstract String getPageName();
}
